package stu.lee.controller;


import com.sun.deploy.net.HttpResponse;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import stu.lee.entity.Admin;
import stu.lee.service.IAdminService;
import stu.lee.service.RedisService;
import stu.lee.stu.lee.core.common.Utils.EnCodeByMd5;
import test.Singnal;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

@org.springframework.stereotype.Controller
@RequestMapping("/admin")
@SessionAttributes("user")
public class AdminController {
    @Resource
    private IAdminService adminService;
    @Resource
    private ServletContext application;

    @RequestMapping(value = "/showAllAdmin")
    @ResponseBody
    public List<Admin> showAllAdmin() {
        return adminService.showAllAdmin();
    }

    @RequestMapping(value = "/login")
    public String login(Admin admin, HttpSession session) {
        try {

            List<Admin> login = adminService.login(admin);
            if (login.size() > 0) {
                System.out.println("login.size() = " + login.size());
                session.setAttribute("user", login.get(0));
                return "forward:/index.jsp";
            } else {
                System.out.println("AdminController.login");
                return "ERROR";
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            return "forward:/index.jsp";
        }
    }

    @RequestMapping(value = "/register")
    public String register(Admin admin) {
        if (admin != null) {
            admin.setId(UUID.randomUUID().toString().replace("-", ""));
            try {
                admin.setPwd(EnCodeByMd5.EncoderByMd5(admin.getPwd().toString()));
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            System.out.println("admin = " + admin);
            adminService.register(admin);
        }
        return "forward:/index.jsp";
    }

    @Test
    public void te() {
//        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
//        RedisService redisService = (RedisService) app.getBean("redisService");
//        System.out.println(redisService.ping());
//        redisService.set("location","luoyang");

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println(Singnal.getInstance().hashCode());
                }
            }
        }).start();
        Integer i = Integer.MAX_VALUE;
        if (i + 1 < i) {
            System.out.println("ffdf" + i);
        }
    }



    @RequestMapping(value = "/text/{var}", produces = "text/plain;chartset=UTF-8")
    public String show(@PathVariable String var) {
        return var;
    }
}